{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fc3024e2",
   "metadata": {},
   "source": [
    "# Lesson 3: Reflection and Blogpost Writing"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b0cc42f",
   "metadata": {},
   "source": [
    "## Setup"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "96d39be0-eaf3-456d-8613-ba21099ed36b",
   "metadata": {
    "height": 30
   },
   "outputs": [],
   "source": [
    "llm_config = {\"model\": \"gpt-3.5-turbo\"}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0969e6bb",
   "metadata": {},
   "source": [
    "## The task!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8074032-3690-4de9-ad08-ea8323cb441b",
   "metadata": {
    "height": 114
   },
   "outputs": [],
   "source": [
    "task = '''\n",
    "        Write a concise but engaging blogpost about\n",
    "       DeepLearning.AI. Make sure the blogpost is\n",
    "       within 100 words.\n",
    "       '''\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1987f023",
   "metadata": {},
   "source": [
    "## Create a writer agent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe0f0a47-a9fe-43a0-b7b1-79922e4c4ac8",
   "metadata": {
    "height": 199
   },
   "outputs": [],
   "source": [
    "import autogen\n",
    "\n",
    "writer = autogen.AssistantAgent(\n",
    "    name=\"Writer\",\n",
    "    system_message=\"You are a writer. You write engaging and concise \" \n",
    "        \"blogpost (with title) on given topics. You must polish your \"\n",
    "        \"writing based on the feedback you receive and give a refined \"\n",
    "        \"version. Only return your final work without additional comments.\",\n",
    "    llm_config=llm_config,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c7b4d8d-40f7-4a05-8958-25d20054de3a",
   "metadata": {
    "height": 46
   },
   "outputs": [],
   "source": [
    "reply = writer.generate_reply(messages=[{\"content\": task, \"role\": \"user\"}])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c501c97d-e338-4f36-a384-6ec45983cf77",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "print(reply)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49658114",
   "metadata": {},
   "source": [
    "## Adding reflection \n",
    "\n",
    "Create a critic agent to reflect on the work of the writer agent."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c7fcd1c7-51ec-4915-8e97-bac03565c4c7",
   "metadata": {
    "height": 165
   },
   "outputs": [],
   "source": [
    "critic = autogen.AssistantAgent(\n",
    "    name=\"Critic\",\n",
    "    is_termination_msg=lambda x: x.get(\"content\", \"\").find(\"TERMINATE\") >= 0,\n",
    "    llm_config=llm_config,\n",
    "    system_message=\"You are a critic. You review the work of \"\n",
    "                \"the writer and provide constructive \"\n",
    "                \"feedback to help improve the quality of the content.\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "899d5fdb-6081-470b-b287-8cf8b8142d0d",
   "metadata": {
    "height": 114
   },
   "outputs": [],
   "source": [
    "res = critic.initiate_chat(\n",
    "    recipient=writer,\n",
    "    message=task,\n",
    "    max_turns=2,\n",
    "    summary_method=\"last_msg\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7b76449",
   "metadata": {},
   "source": [
    "## Nested chat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "401ecf92-63e9-40ff-aeed-1c404352e4ab",
   "metadata": {
    "height": 216
   },
   "outputs": [],
   "source": [
    "SEO_reviewer = autogen.AssistantAgent(\n",
    "    name=\"SEO Reviewer\",\n",
    "    llm_config=llm_config,\n",
    "    system_message=\"You are an SEO reviewer, known for \"\n",
    "        \"your ability to optimize content for search engines, \"\n",
    "        \"ensuring that it ranks well and attracts organic traffic. \" \n",
    "        \"Make sure your suggestion is concise (within 3 bullet points), \"\n",
    "        \"concrete and to the point. \"\n",
    "        \"Begin the review by stating your role.\",\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f85acb81-7ab9-4c84-b8bb-6fbae3dce848",
   "metadata": {
    "height": 199
   },
   "outputs": [],
   "source": [
    "legal_reviewer = autogen.AssistantAgent(\n",
    "    name=\"Legal Reviewer\",\n",
    "    llm_config=llm_config,\n",
    "    system_message=\"You are a legal reviewer, known for \"\n",
    "        \"your ability to ensure that content is legally compliant \"\n",
    "        \"and free from any potential legal issues. \"\n",
    "        \"Make sure your suggestion is concise (within 3 bullet points), \"\n",
    "        \"concrete and to the point. \"\n",
    "        \"Begin the review by stating your role.\",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d46a177a-8088-4956-8d2b-3e916b8ca5e9",
   "metadata": {
    "height": 199
   },
   "outputs": [],
   "source": [
    "ethics_reviewer = autogen.AssistantAgent(\n",
    "    name=\"Ethics Reviewer\",\n",
    "    llm_config=llm_config,\n",
    "    system_message=\"You are an ethics reviewer, known for \"\n",
    "        \"your ability to ensure that content is ethically sound \"\n",
    "        \"and free from any potential ethical issues. \" \n",
    "        \"Make sure your suggestion is concise (within 3 bullet points), \"\n",
    "        \"concrete and to the point. \"\n",
    "        \"Begin the review by stating your role. \",\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7b2ad6f-8ba6-436a-9459-14ffbe8a32d3",
   "metadata": {
    "height": 131
   },
   "outputs": [],
   "source": [
    "meta_reviewer = autogen.AssistantAgent(\n",
    "    name=\"Meta Reviewer\",\n",
    "    llm_config=llm_config,\n",
    "    system_message=\"You are a meta reviewer, you aggragate and review \"\n",
    "    \"the work of other reviewers and give a final suggestion on the content.\",\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "913beca1",
   "metadata": {},
   "source": [
    "## Orchestrate the nested chats to solve the task"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a11a70c7-19ca-4e5a-ad3d-f2b481fb5915",
   "metadata": {
    "height": 556
   },
   "outputs": [],
   "source": [
    "def reflection_message(recipient, messages, sender, config):\n",
    "    return f'''Review the following content. \n",
    "            \\n\\n {recipient.chat_messages_for_summary(sender)[-1]['content']}'''\n",
    "\n",
    "review_chats = [\n",
    "    {\n",
    "     \"recipient\": SEO_reviewer, \n",
    "     \"message\": reflection_message, \n",
    "     \"summary_method\": \"reflection_with_llm\",\n",
    "     \"summary_args\": {\"summary_prompt\" : \n",
    "        \"Return review into as JSON object only:\"\n",
    "        \"{'Reviewer': '', 'Review': ''}. Here Reviewer should be your role\",},\n",
    "     \"max_turns\": 1},\n",
    "    {\n",
    "    \"recipient\": legal_reviewer, \"message\": reflection_message, \n",
    "     \"summary_method\": \"reflection_with_llm\",\n",
    "     \"summary_args\": {\"summary_prompt\" : \n",
    "        \"Return review into as JSON object only:\"\n",
    "        \"{'Reviewer': '', 'Review': ''}.\",},\n",
    "     \"max_turns\": 1},\n",
    "    {\"recipient\": ethics_reviewer, \"message\": reflection_message, \n",
    "     \"summary_method\": \"reflection_with_llm\",\n",
    "     \"summary_args\": {\"summary_prompt\" : \n",
    "        \"Return review into as JSON object only:\"\n",
    "        \"{'reviewer': '', 'review': ''}\",},\n",
    "     \"max_turns\": 1},\n",
    "     {\"recipient\": meta_reviewer, \n",
    "      \"message\": \"Aggregrate feedback from all reviewers and give final suggestions on the writing.\", \n",
    "     \"max_turns\": 1},\n",
    "]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3a40b66-5061-460d-ad9d-c0dbcfbba2e9",
   "metadata": {
    "height": 80
   },
   "outputs": [],
   "source": [
    "critic.register_nested_chats(\n",
    "    review_chats,\n",
    "    trigger=writer,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43b8797d",
   "metadata": {},
   "source": [
    "**Note**: You might get a slightly different response than what's shown in the video. Feel free to try different task."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b8dcac3-1e72-43b7-9d5a-1be740f6efd5",
   "metadata": {
    "height": 114
   },
   "outputs": [],
   "source": [
    "res = critic.initiate_chat(\n",
    "    recipient=writer,\n",
    "    message=task,\n",
    "    max_turns=2,\n",
    "    summary_method=\"last_msg\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5c833b0",
   "metadata": {},
   "source": [
    "## Get the summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68ef82ed-f102-4964-b7be-60e2f258a39b",
   "metadata": {
    "height": 29
   },
   "outputs": [],
   "source": [
    "print(res.summary)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
